#define _GNU_SOURCE
#define __USE_GNU
#include <unistd.h>
//#include <linux/futex.h>
//#include <time.h>
#include <sys/time.h>
#include <sys/syscall.h>
#include <sched.h>
#include <errno.h>

#include <malloc.h>

int target(void *arg) {
    sg_write(2, "target was run\n", sizeof("target was run\n"));
    _exit(0);
    //return 0;
}

struct timespec t = {10, 0};
//I don't think it would hurt to make this void*,
//but void** is what's used in http://www.linuxjournal.com/article/5211
void** stack;

int main(int argc, char* argv[]) {
    int res;
    stack = (void**)malloc(16384);
    
    res = __clone(target, 
                  stack,
                  CLONE_THREAD|CLONE_SIGHAND|CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_PARENT,
                  NULL //fn arg
                  );
    if(res)
        perror("Error creating process");
    //sleep for 10sec
    syscall(SYS_nanosleep, &t, NULL);
    sg_write(2, "sleep finished\n", sizeof("sleep finished\n"));
    return 0;
}